package worker

import (
	"NovelBook/logic/book"
	"NovelBook/models/res"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
)

// BorrowBooks Worker处的借书
// 相当于模拟图书卡、扫码，获取 borrower_id 后来借书
func BorrowBooks(c *gin.Context) {
	idAny, exist := c.Get(auth.WorkerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var req res.BorrowBook

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	// 借书操作
	err := book.BorrowBooksByUniqueID(req.UserId, req.BookUniqueIDs)
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, err.Error())
		return
	}

	response.Success(c, nil)
}
